{include file="public/head"}
<div class="main">
    {include file="public/left"}
    <div class="middle">
    {include file="public/header"}
    <div class='userinfo'>
        {include file="public/userleft"}
        <ul class='info_base'>
            <dl class="user_title">{:__('基本资料')}</dl>
            <dl>
                <dd class='usertag'>{:__('用户名')}：</dd>
                <dd id="username"></dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('头像')}：</dd>
                <dd>
                    <p><img src="" width="60px" height="60px"  id="avatarurl" onerror="changeimg(this)"/></p>
                    <p style="margin-top: 2px;margin-bottom: 2px;">
                        <input style="display: none;" id="avatar" type="file"/>
                        <button id="avatar_copy" style="margin-left: 0px;" class="layui-btn layui-btn-primary">{:__("选择文件")}</button><span id="filename">{:__("请选择上传的文件")}</span>
                    </p>
                    <span>({:__('允许上传附件格式')}：jpg,gif,png。{:__('上限')}:10M)</span>
                </dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('性别')}：</dd>
                <dd class="layui-form">
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="1" title="{:__('男')}">
                            <input type="radio" name="sex" value="2" title="{:__('女')}">
                        </div>
                    </div>
                </dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('真实姓名')}：</dd>
                <dd>
                    <input id='truename' class="layui-input" type="text"/>
                </dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('出生日期')}：</dd>
                <dd>
                    <input id='birthday' placeholder="{:__('年-月-日')}" readonly class="layui-input" type="text"/>
                </dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('所在地')}：</dd>
                <dd>
                    <input id='address' class="layui-input" type="text" placeholder="{:__('格式a-b-c')}"/>
                </dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('个人简介')}：</dd>
                <dd>
                    <textarea id="intro" name="desc" class="layui-textarea"></textarea>
                </dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('从业经历')}：</dd>
                <dd>
                    <textarea id="jobs" class="layui-textarea"></textarea>
                </dd>
            </dl>
            <dl class="user_title">{:__('联系方式')}</dl>
            <dl>
                <dd class='usertag'>{:__('E-mail')}：</dd>
                <dd>
                    <input id="email" data-title='{:__("邮箱已认证不能更改")}'  class="layui-input" type="text"/>
                </dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('MSN')}：</dd>
                <dd >
                    <input id="msn" class="layui-input" type="text"/>
                </dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('手机')}：</dd>
                <dd>
                    <input id="phone" class="layui-input" type="text"/>
                </dd>
            </dl>
            <dl class="user_title">{:__('收款信息')}</dl>
            <dl>
                <dd class='usertag'>{:__('银行')}：</dd>
                <dd>
                    <textarea placeholder="{:__('所属银行:xxx;银行帐号:xxx;开户人姓名:xxx;')}" id="bank_info" class="layui-textarea"></textarea>
                </dd>
            </dl>
            <dl>
                <dd class='usertag'>{:__('注册时间')}：</dd>
                <dd id="create_time"></dd>
            </dl>
            <dl>
                <button type="button" id="user_sub" class="layui-btn layui-btn-warm">{:__('确认修改')}</button>
            </dl>
        </ul>
    </div>
    {include file="public/foot"}
    </div>
    {include file="public/right"}
</div>
{include file="public/footer"}
<script>
    noLog();
    $(function(){
        layui.use('laydate', function(){
            var laydate = layui.laydate;
            var lang = $.cookie("think_lang");
            var setlang = '';
            if(lang == 'en-us'){
                setlang = 'en';
            } else if(lang == 'zh-cn'){
                setlang= 'cn';
            }
            //执行一个laydate实例
            laydate.render({
                elem: '#birthday', //指定元素
                type:'date',
                lang:setlang
            });
        });
        $(document).queue('user',function(){
            var userData = getLocalStorage('userData');
            var userdata = JSON.parse(userData);
            $("#username").html(userdata['username']);
            $("#create_time").html(userdata['create_time']);
            if(userdata['profile']){
                 
               // $("#avatarurl").attr('src',userdata['profile']['avatar']);
                 
                if(userdata['profile']['truename'] != '' && userdata['profile']['truename'] != null && userdata['profile']['truename'] != undefined){
                    $("#truename").val(userdata['profile']['truename']);
                }
                if(userdata['profile']['birthday'] != '' && userdata['profile']['birthday'] != null && userdata['profile']['birthday'] != undefined){
                    var birthdaycopy = userdata['profile']['birthday'];
                    var date = new Date(birthdaycopy);
                    var year = date.getFullYear();
                    var month = parseInt(date.getMonth())+1;
                    if(month<10){
                        month = '0'+month;
                    }
                    var day = parseInt(date.getDate());
                    if(day<10){
                        day = '0'+day;
                    }
                    var birthday = year+'-'+month+'-'+day;
                    $("#birthday").val(birthday);
                }
                if(userdata['profile']['address'] != '' && userdata['profile']['address'] != null && userdata['profile']['address'] != undefined){
                    $("#address").val(userdata['profile']['address']);
                }
                if(userdata['profile']['intro'] != '' && userdata['profile']['intro'] != null && userdata['profile']['intro'] != undefined){
                    $("#intro").val(userdata['profile']['intro']);
                }
                if(userdata['profile']['jobs'] != '' && userdata['profile']['jobs'] != null && userdata['profile']['jobs'] != undefined){
                    $("#jobs").val(userdata['profile']['jobs']);
                }
                if(userdata['profile']['msn'] != '' && userdata['profile']['msn'] != null && userdata['profile']['msn'] != undefined){
                    $("#msn").val(userdata['profile']['msn']);
                }
                if(userdata['profile']['bank_info'] != '' && userdata['profile']['bank_info'] != null && userdata['profile']['bank_info'] != undefined){
                    $("#bank_info").val(userdata['profile']['bank_info']);
                }
            }
            if(userdata['email'] != '' && userdata['email'] != null && userdata['email'] != undefined){
                $("#email").val(userdata['email']);
            }
            if(userdata['emailauth'] != '' && userdata['emailauth'] != undefined && userdata['emailauth'] == 1){
                $(document).on("mouseover","#email",function(){
                    var _this = $(this);
                    var title = _this.attr("data-title");
                    layer.tips(title,this, 
                    {tips:1});
                });
                $("#email").attr("disabled",true);
                $("#email").css("background","#eeeeee");
            }
            if(userdata['phone'] != '' && userdata['phone'] != null && userdata['phone'] != undefined){
                $("#phone").val(userdata['phone']);
            }
    
            if(userdata['sex'] != '' && userdata['sex'] != null && userdata['sex'] != undefined){
                if(userdata['sex'] == '1'){
                    $($("input[name=sex]").siblings("div")[0]).find('i').trigger('click');
                } else {
                    $($("input[name=sex]").siblings("div")[1]).find('i').trigger('click');
                }
            }
            
        });
        
    });
    
    $("#avatar").change(function(){
        var formdata = new FormData();
        var files = $("#avatar")[0].files[0];

        formdata.append("file",files);
        formdata.append('file_type','image');
        formdata.append('token',token);
        console.log(formdata);
        $.ajax({
            url:'/api/common/upload',
            type:'post',
            data:formdata,
            processData:false,
            contentType:false,
            success:function(ret){
                if(ret.code != 401 && ret.code != 1){
                    layer.msg(ret.msg,{icon:2});
                } else if(ret.code ==1){
                    layer.msg(ret.msg,{icon:1},function(){
                        if(ret['data']['file'] != '' && ret['data']['file'] != null && ret['data']['file'] != undefined){
                            $("#avatarurl").attr("src",ret['data']['file']['file_path']);
                            $("#filename").html(ret['data']['file']['file_name']);
                        }
                    });
                }
            }
        });
    });
    $("#user_sub").click(function(){
        var avatar =$("#avatarurl").prop('src');
        var sex = $("input[name=sex]:checked").val();
        var truename = $("#truename").val();
        var birthdaycopy = $("#birthday").val();
        var birthday = '';
        if(birthdaycopy != ''){
            var date = new Date(birthdaycopy);
            var year = date.getFullYear();
            var month = parseInt(date.getMonth())+1;
            if(month<10){
                month = '0'+month;
            }
            var day = parseInt(date.getDate());
            if(day<10){
                day = '0'+day;
            }
            birthday = year+'-'+month+'-'+day;
        }
        var address = $("#address").val();
        var intro = $("#intro").val();
        var jobs = $("#jobs").val();
        var email = $("#email").val();
        var msn = $("#msn").val();
        var phone = $("#phone").val();
        var bank_info = $("#bank_info").val();
        if(emailCheck(email) === false){
            layer.msg('{:__("Email格式错误")}',{icon:2});
            return false;
        }
        if(phone != '' && telCheck(phone) === false){
            layer.msg('{:__("手机格式错误")}',{icon:2});
            return false;
        }
        $.ajax({
            url:'/api/user/changeProfile',
            type:'post',
            data:{'token':token,'sex':sex,'avatar':avatar,'truename':truename,'birthday':birthday,'address':address,'intro':intro,'jobs':jobs,'email':email,'msn':msn,'phone':phone,'bank_info':bank_info},
            success:function(ret){
                if(ret.code !=1 && ret.code != 401){
                    layer.msg(ret.msg,{icon:2});
                } else if(ret.code == 1){
                    layer.msg(ret.msg,{icon:1},function(){
                        objs = {'userData':1}
                        CacheLoad(objs);
                        $(document).queue("user",function(){
                          //  window.location.href = "/index/User/updateinfo";
                        });
                    });
                }
            }
        });
    });
    $("#avatar_copy").click(function(){
        $("#avatar").trigger("click");
    });
</script>